package com.LeetCode;

public class _14_main {
	public static String longestCommonPrefix(String[] strs) {
		if (strs == null || strs.length == 0) {
			return "";
		}
		String str1 = strs[0];
		if (strs.length== 1)
			return str1;
		if (str1.length() == 0)
			return "";
		String maxstr = "";
		for (int j = 1; j <= str1.length(); j++) {
			String subs = str1.substring(0, j);
			boolean flag = true;
			for (int k = 1; k < strs.length; k++) {
				if (!strs[k].startsWith(subs)) {
					flag = false;
					break;
				}
			}
			if (flag)
				maxstr = subs;
			else
				break;
		}
		return maxstr;
	}

	public static void main(String[] args) {
		String[] strs1 = {};
		String[] strs2 = {""};
		String[] strs3 = { "abcdef"," a"};
		//System.out.println(longestCommonPrefix(strs1));
		//System.out.println(longestCommonPrefix(strs2));
		System.out.println(longestCommonPrefix(strs3));

	}
}
